package com.woniuxy.springbootrabbitconsumer.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Author: linKe
 * @Date: 2022/1/12 10:49
 * @FileName: RabbitMqConfiguration
 * @version: 1.0
 * @Description: RabbitMq配置类，定义交换机，队列，绑定
 */
@Configuration
public class RabbitMqConfiguration {
    /**
     * 定义交换机
     * @return  结果
     */
    @Bean
    public TopicExchange exchangeTopic() {
        TopicExchange exchangeTopic = new TopicExchange("exchangeTopic");
        // 交换机开启延时队列
        exchangeTopic.setDelayed(true);
        return    exchangeTopic;
    }
    /**
     * 定义死信前置交换机
     * @return  结果
     */
    @Bean
    public TopicExchange exchangeTrue() {
        return new TopicExchange("exchangeTrue");
    }
    /**
     * 定义死信处理交换机
     * @return  结果
     */
    @Bean
    public TopicExchange exchangeDeadLx() {
        return new TopicExchange("exchangeDeadLx");
    }
    /**
     * 定义队列
     * @return 结果
     */
    @Bean
    public Queue queueEmailTopic(){
        return new Queue("queueEmailTopic");
    }
    /**
     * 定义队列
     * @return 结果
     */
    @Bean
    public Queue queueSmsTopic(){
        return new Queue("queueSmsTopic");
    }
    /**
     * 定义死信队列的前置队列
     * @return 结果
     */
    @Bean
    public Queue queueEmailTrueTest(){
        HashMap<String, Object>  arguments= new HashMap<>(5);
        // 设置队列最大存放数量
        arguments.put("x-max-length", 20);
        // 死信交换机设置
        arguments.put("x-dead-letter-exchange","exchangeDeadLx");
        // 死信路由配置
        arguments.put("x-dead-letter-routing-key","deadLetterX");
        return new Queue("queueEmailTrueTest",true,false,false,arguments);
    }
    /**
     * 定义死信队列的接收队列
     * @return 结果
     */
    @Bean
    public Queue queueDeadLxTest(){
        return new Queue("queueDeadLxTest");
    }

/*    *//**
     * 绑定队列到交换机
     * @return  结果
     *//*
    @Bean
    public Binding binding(){
        // spring 默认是按byType注入 调用bean方式进行设置
        return BindingBuilder.bind(queueEmailTopic()).to(exchangeTopic()).with("#.email.#");
    }*/
    /**
     * 绑定队列到交换机
     * 同类型的bean有多个 参数注入
     * ·@Qualifier("queueEmailTopic") Queue queueEmailTopic
     *
     * @return  结果
     */
    @Bean
    public Binding bindingEmail( @Qualifier("exchangeTopic")TopicExchange exchangeTopic, @Qualifier("queueEmailTopic") Queue queueEmailTopic){
        // spring 默认是按byType注入 调用bean方式进行设置
        return BindingBuilder.bind(queueEmailTopic).to(exchangeTopic).with("#.email.#");
    }

    /**
     * 绑定队列到交换机
     * @return  结果
     */
    @Bean
    public Binding bindingSms(@Qualifier("exchangeTopic")TopicExchange exchangeTopic, @Qualifier("queueSmsTopic") Queue queueSmsTopic){
        // spring 默认是按byType注入 调用bean方式进行设置
        return BindingBuilder.bind(queueSmsTopic).to(exchangeTopic).with("#.sms.#");
    }
    /**
     * 绑定队列到死信前置交换机
     * @return  结果
     */
    @Bean
    public Binding bindingDeadSms(@Qualifier("exchangeTrue")TopicExchange exchangeTopic, @Qualifier("queueEmailTrueTest") Queue queueSmsTopic){
        // spring 默认是按byType注入 调用bean方式进行设置
        return BindingBuilder.bind(queueSmsTopic).to(exchangeTopic).with("#.deadLx.#");
    }
    /**
     * 绑定队列到死信处理交换机
     * @return  结果
     */
    @Bean
    public Binding bindingDeadLxSms(@Qualifier("exchangeDeadLx")TopicExchange exchangeTopic, @Qualifier("queueDeadLxTest") Queue queueSmsTopic){
        // spring 默认是按byType注入 调用bean方式进行设置
        return BindingBuilder.bind(queueSmsTopic).to(exchangeTopic).with("#.deadLetterX.#");
    }

}
